package com.yanqu.road.server.manger.maidian.pet;

import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.config.vehicle.VehicleInfo;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.entity.player.UserVehicleOtherSkill;
import com.yanqu.road.entity.player.UserVehicleSkill;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.VehicleMgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 珍兽埋点
 */
public class PetLog {

    public static void trackPetInfoSnapshot(GamePlayer player){
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;

            VehicleModule vehicleModule = player.getModule(VehicleModule.class);

            //公共事件属性
            DataBuilder common = DataBuilder.newBuilder(player.getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
            DataAnalyticsMgr.buildPublicEventData(player, common);

            for (Map.Entry<String, UserVehicle> vehicleEntry : vehicleModule.getUserVehicleMap().entrySet()) {
                UserVehicle userVehicle = vehicleEntry.getValue();
                //珍兽配置
                VehicleInfo vehicleInfo = VehicleMgr.getVehicleInfo(userVehicle.getTemplateId());
                //控制下品质
                if(vehicleInfo.getQuality() < 6){
                    continue;
                }
                if(userVehicle.getPatronsId() <= 0){
                    continue;
                }
                DataBuilder dataBuilder = DataBuilder.newBuilder(player.getUserInfo().getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));

                dataBuilder.build(EventDataConstants.Vehicle.PET_ID, userVehicle.getVehicleUid());
                dataBuilder.build(EventDataConstants.Vehicle.PET_NAME, userVehicle.getTemplateId());
                dataBuilder.build(EventDataConstants.Vehicle.PET_LEVEL, userVehicle.getLevel());

                dataBuilder.build(EventDataConstants.Vehicle.PET_QUALITY, vehicleInfo.getQuality());
                // 计算
                long qualification = VehicleMgr.getVehicleQualification(userVehicle.getTemplateId(), userVehicle.getLevel());
                dataBuilder.build(EventDataConstants.Vehicle.PET_INTELLIGENCE, qualification);
                dataBuilder.build(EventDataConstants.Vehicle.PET_GUEST, userVehicle.getPatronsId());

                //增加赚钱百分比
                int earnAddition = 0;
                //当前解锁技能个数
                int positionNum = 0;
                //满级个数
                int fullNum = 0;
                for (Map.Entry<Integer, UserVehicleSkill> vehicleSkillEntry : userVehicle.getSkillMapEffect().entrySet()) {
                    UserVehicleSkill userVehicleSkill = vehicleSkillEntry.getValue();
                    earnAddition += userVehicleSkill.getValue();
                    positionNum++;
                    if(userVehicleSkill.getValue() >= 25){
                        fullNum++;
                    }
                }
                dataBuilder.build(EventDataConstants.Vehicle.PET_ADD, earnAddition);

                //光环
                dataBuilder.build(EventDataConstants.Vehicle.PET_HALO, PetLog.getOtherSkillArray(userVehicle));

                //技能
                //觉醒等级
                int maxStudyLv = 0;
                //觉醒技能类型
                for (int type : vehicleInfo.getStudyTypeList()){
                    maxStudyLv += VehicleMgr.getVehicleStudy(type).size();
                }
                //技能上限数量
                int maxSkillNum = vehicleInfo.getDefaultSkillCount() + maxStudyLv;
                //当前解锁技能个数
//                int positionNum = vehicleInfo.getDefaultSkillCount() + userVehicle.getStudyLv();
                List<Integer> skillNumList = new ArrayList<>();
                skillNumList.add(maxSkillNum);
                skillNumList.add(positionNum);
                skillNumList.add(fullNum);
                dataBuilder.build(EventDataConstants.Vehicle.PET_SKILL, skillNumList);

                dataBuilder.getDataMap().putAll(common.getDataMap());
                DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), EventDataConstants.Vehicle.PET_INFO, dataBuilder.getDataMap());
            }

        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }
    }

    /**
     * 获取光环技能
     * @param userVehicle
     * @return
     */
    public static List<Map<String, Object>> getOtherSkillArray(UserVehicle userVehicle){
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (UserVehicleOtherSkill vehicleOtherSkill : new ArrayList<>(userVehicle.getOtherSkillList())) {
            Map<String, Object> otherSkillObject = new HashMap<>();
            otherSkillObject.put(EventDataConstants.Vehicle.PET_HALOID, vehicleOtherSkill.getSkillId());
            otherSkillObject.put(EventDataConstants.Vehicle.PET_HALOTYPE, vehicleOtherSkill.getSkillType());
            otherSkillObject.put(EventDataConstants.Vehicle.PET_HALOLV, vehicleOtherSkill.getSkillLv());
            resultList.add(otherSkillObject);
        }
        return resultList;
    }

}
